import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.TreeMap;

public class DSMemoryTracker {

	private final static int DATASET_SIZE = 1000000;

	public static void main(String[] args) throws InterruptedException {
		System.gc();
		Thread.sleep(5);
		System.gc();

		long m;

		System.gc();
		m = Runtime.getRuntime().freeMemory();
		int[] array = new int[DATASET_SIZE];
		for (int i = 0; i < DATASET_SIZE; i++) {
			array[i] = (int) (Math.random() * DATASET_SIZE);
		}
		m = m - Runtime.getRuntime().freeMemory();
		System.out.println("int[], total size = " + m + ", per element: " + m
				/ DATASET_SIZE);

		System.gc();
		m = Runtime.getRuntime().freeMemory();
		ArrayList<Integer> arrayList = new ArrayList<>(DATASET_SIZE);
		for (int i = 0; i < DATASET_SIZE; i++) {
			arrayList.add(new Integer((int) (Math.random() * DATASET_SIZE)));
		}
		m = m - Runtime.getRuntime().freeMemory();
		System.out.println("ArrayList<Integer>, total size = " + m
				+ ", per element: " + m / DATASET_SIZE);

		System.gc();
		m = Runtime.getRuntime().freeMemory();
		LinkedList<Integer> linkedList = new LinkedList<>();
		for (int i = 0; i < DATASET_SIZE; i++) {
			linkedList.add(new Integer((int) (Math.random() * DATASET_SIZE)));
		}
		m = m - Runtime.getRuntime().freeMemory();
		System.out.println("LinkedList<Integer>, total size = " + m
				+ ", per element: " + m / DATASET_SIZE);

		System.gc();
		m = Runtime.getRuntime().freeMemory();
		HashMap<Integer, Integer> hashMap = new HashMap<>(
				(int) (DATASET_SIZE / 0.7));
		for (int i = 0; i < DATASET_SIZE; i++) {
			hashMap.put(new Integer((int) (Math.random() * DATASET_SIZE)),
					new Integer((int) (Math.random() * DATASET_SIZE)));
		}
		m = m - Runtime.getRuntime().freeMemory();
		System.out.println("HashMap<Integer, Integer>, total size = " + m
				+ ", per pair: " + m / DATASET_SIZE);

		System.gc();
		m = Runtime.getRuntime().freeMemory();
		TreeMap<Integer, Integer> treeMap = new TreeMap<>();
		for (int i = 0; i < DATASET_SIZE; i++) {
			treeMap.put(new Integer((int) (Math.random() * DATASET_SIZE)),
					new Integer((int) (Math.random() * DATASET_SIZE)));
		}
		m = m - Runtime.getRuntime().freeMemory();
		System.out.println("TreeMap<Integer, Integer>, total size = " + m
				+ ", per pair: " + m / DATASET_SIZE);
	}
}
